package cn.edu.nuaa.aadl2.generatorC.utils;

import cn.edu.nuaa.aadl2.generatorC.utils.StringUtils;
import com.google.common.base.Objects;
import java.util.List;
import org.eclipse.emf.common.util.EList;
import org.eclipse.xtext.xbase.lib.Conversions;
import org.osate.aadl2.Classifier;
import org.osate.aadl2.Feature;
import org.osate.aadl2.FeatureClassifier;
import org.osate.aadl2.SubprogramClassifier;
import org.osate.aadl2.impl.DataAccessImpl;
import org.osate.aadl2.impl.DataImplementationImpl;
import org.osate.aadl2.impl.DataTypeImpl;
import org.osate.aadl2.impl.NamedElementImpl;
import org.osate.aadl2.impl.ParameterImpl;
import org.osate.aadl2.impl.SubprogramAccessImpl;

@SuppressWarnings("all")
public class ParamUtils {
  public static String convert(final String str) {
    boolean _equals = Objects.equal(str, null);
    if (_equals) {
      return "";
    }
    return str.toLowerCase().replace(".", "_");
  }
  
  public static String transDataAADL2C(final String aadl) {
    if (aadl != null) {
      switch (aadl) {
        case "Character":
          return "char";
        case "Boolean":
          return "bool";
        case "Integer":
          return "int";
        case "Integer_32":
          return "int";
        case "Unsigned_16":
          return "short";
        case "Unsigned_32":
          return "int";
        case "Unsigned_64":
          return "long";
        case "Float":
          return "float";
        case "Double":
          return "double";
        case "void":
          return "void";
        default:
          return StringUtils.convertNo2Low(aadl);
      }
    } else {
      return StringUtils.convertNo2Low(aadl);
    }
  }
  
  public static String getReturnType(final Classifier classifier) {
    String returnType = "";
    EList<Feature> features = classifier.getAllFeatures();
    String result = "void";
    for (int i = 0; (i < features.size()); i++) {
      {
        Feature oneFeature = features.get(i);
        FeatureClassifier type = features.get(i).getFeatureClassifier();
        if (((oneFeature instanceof ParameterImpl) && (type instanceof NamedElementImpl))) {
          boolean _isOut = ((ParameterImpl) oneFeature).isOut();
          if (_isOut) {
            returnType = ((NamedElementImpl) type).getName();
            return StringUtils.convertNo2Low(ParamUtils.transDataAADL2C(returnType));
          }
        } else {
          return StringUtils.convertNo2Low(ParamUtils.transDataAADL2C(result));
        }
      }
    }
    return StringUtils.convertNo2Low(ParamUtils.transDataAADL2C(result));
  }
  
  @Deprecated
  public static String getReturnType(final Feature feature) {
    String paras = "";
    EList<Feature> features = feature.getClassifier().getAllFeatures();
    String result = "void";
    for (int i = 0; (i < features.size()); i++) {
      {
        Feature oneFeature = features.get(i);
        FeatureClassifier type = features.get(i).getFeatureClassifier();
        if (((oneFeature instanceof ParameterImpl) && (type instanceof DataTypeImpl))) {
          boolean _isOut = ((ParameterImpl) oneFeature).isOut();
          if (_isOut) {
            return ((DataTypeImpl) type).getName();
          }
        } else {
          return result;
        }
      }
    }
    return result;
  }
  
  public static String getAllDataAccess(final Classifier classifier) {
    String paras = "";
    EList<Feature> features = classifier.getAllFeatures();
    String result = "";
    for (int i = 0; (i < features.size()); i++) {
      {
        Feature oneFeature = features.get(i);
        if ((!(oneFeature instanceof DataAccessImpl))) {
          features.remove(i);
          i--;
        } else {
          String name = oneFeature.getName();
          FeatureClassifier type = oneFeature.getFeatureClassifier();
          String typeName = ((DataImplementationImpl) type).getName();
          int _size = features.size();
          int _minus = (_size - 1);
          boolean _notEquals = (i != _minus);
          if (_notEquals) {
            String _result = result;
            String _replace = typeName.replace(".", "_");
            String _plus = ("extern " + _replace);
            String _plus_1 = (_plus + " ");
            String _plus_2 = (_plus_1 + name);
            String _plus_3 = (_plus_2 + ";\n");
            result = (_result + _plus_3);
          } else {
            String _result_1 = result;
            String _replace_1 = typeName.replace(".", "_");
            String _plus_4 = ("extern " + _replace_1);
            String _plus_5 = (_plus_4 + " ");
            String _plus_6 = (_plus_5 + name);
            String _plus_7 = (_plus_6 + ";");
            result = (_result_1 + _plus_7);
          }
        }
      }
    }
    return StringUtils.convertNo2Low(result);
  }
  
  public static String getAllSubProgram(final Classifier classifier) {
    String paras = "";
    EList<Feature> features = classifier.getAllFeatures();
    String result = "";
    for (int i = 0; (i < features.size()); i++) {
      {
        Feature oneFeature = features.get(i);
        if ((!(oneFeature instanceof SubprogramAccessImpl))) {
          features.remove(i);
          i--;
        } else {
          String name = oneFeature.getName();
          FeatureClassifier type = oneFeature.getFeatureClassifier();
          SubprogramClassifier subProgramClassifier = ((SubprogramClassifier) type);
          String returnType = ParamUtils.getReturnType(subProgramClassifier);
          String paramsList = ParamUtils.getParasList(subProgramClassifier);
          int _size = features.size();
          int _minus = (_size - 1);
          boolean _notEquals = (i != _minus);
          if (_notEquals) {
            String _result = result;
            String _convertNo2Low = StringUtils.convertNo2Low(returnType);
            String _plus = ("extern " + _convertNo2Low);
            String _plus_1 = (_plus + " ");
            String _plus_2 = (_plus_1 + name);
            String _plus_3 = (_plus_2 + "(");
            String _plus_4 = (_plus_3 + paramsList);
            String _plus_5 = (_plus_4 + ");");
            String _plus_6 = (_plus_5 + "\n");
            result = (_result + _plus_6);
          } else {
            String _result_1 = result;
            String _convertNo2Low_1 = StringUtils.convertNo2Low(returnType);
            String _plus_7 = ("extern " + _convertNo2Low_1);
            String _plus_8 = (_plus_7 + " ");
            String _plus_9 = (_plus_8 + name);
            String _plus_10 = (_plus_9 + "(");
            String _plus_11 = (_plus_10 + paramsList);
            String _plus_12 = (_plus_11 + ");");
            result = (_result_1 + _plus_12);
          }
        }
      }
    }
    return result;
  }
  
  public static String getParasListWithoutType(final Classifier classifier) {
    String paras = ParamUtils.getParasList(classifier);
    if ((paras.contains("unionContOrder") && paras.contains("ucMisNum"))) {
      int a = 0;
      int b = 1;
    }
    String result = "";
    boolean _equals = paras.equals("");
    boolean _not = (!_equals);
    if (_not) {
      String[] parasList = paras.split(",");
      for (int i = 0; (i < ((List<String>)Conversions.doWrapArray(parasList)).size()); i++) {
        {
          String[] tmp = parasList[i].split(" ");
          final String[] _converted_parasList = (String[])parasList;
          int _size = ((List<String>)Conversions.doWrapArray(_converted_parasList)).size();
          int _minus = (_size - 1);
          boolean _equals_1 = (i == _minus);
          if (_equals_1) {
            String _result = result;
            String _get = tmp[1];
            result = (_result + _get);
          } else {
            String _result_1 = result;
            String _get_1 = tmp[1];
            String _plus = (_get_1 + ",");
            result = (_result_1 + _plus);
          }
        }
      }
      return StringUtils.convertNo2Low(result);
    } else {
      return result;
    }
  }
  
  public static String getParasList(final EList<Feature> features) {
    String result = "";
    for (int i = 0; (i < features.size()); i++) {
      {
        Feature oneFeature = features.get(i);
        if ((oneFeature instanceof ParameterImpl)) {
          boolean _isOut = ((ParameterImpl) oneFeature).isOut();
          if (_isOut) {
            features.remove(i);
            i--;
          }
        } else {
          if ((oneFeature instanceof DataAccessImpl)) {
            features.remove(i);
            i--;
          } else {
            if ((oneFeature instanceof SubprogramAccessImpl)) {
              features.remove(i);
              i--;
            }
          }
        }
      }
    }
    for (int i = 0; (i < features.size()); i++) {
      {
        Feature oneFeature = features.get(i);
        String paraName = features.get(i).getName();
        FeatureClassifier type = features.get(i).getFeatureClassifier();
        String typeName = "";
        if ((type instanceof NamedElementImpl)) {
          typeName = ((NamedElementImpl) type).getName();
          typeName = ParamUtils.transDataAADL2C(typeName);
        } else {
          typeName = type.toString();
        }
        if (((!typeName.equals("")) && (!paraName.equals("")))) {
          int _size = features.size();
          int _minus = (_size - 1);
          boolean _equals = (i == _minus);
          if (_equals) {
            String _result = result;
            String _convertNo2Low = StringUtils.convertNo2Low(typeName);
            String _plus = (_convertNo2Low + " ");
            String _plus_1 = (_plus + paraName);
            result = (_result + _plus_1);
          } else {
            String _result_1 = result;
            String _convertNo2Low_1 = StringUtils.convertNo2Low(typeName);
            String _plus_2 = (_convertNo2Low_1 + " ");
            String _plus_3 = (_plus_2 + paraName);
            String _plus_4 = (_plus_3 + ",");
            result = (_result_1 + _plus_4);
          }
        }
      }
    }
    return StringUtils.convertNo2Low(result);
  }
  
  public static String getParasList(final Classifier classifier) {
    String paras = "";
    EList<Feature> features = classifier.getAllFeatures();
    String result = "";
    for (int i = 0; (i < features.size()); i++) {
      {
        Feature oneFeature = features.get(i);
        if ((oneFeature instanceof ParameterImpl)) {
          boolean _isOut = ((ParameterImpl) oneFeature).isOut();
          if (_isOut) {
            features.remove(i);
            i--;
          }
        } else {
          if ((oneFeature instanceof DataAccessImpl)) {
            features.remove(i);
            i--;
          } else {
            if ((oneFeature instanceof SubprogramAccessImpl)) {
              features.remove(i);
              i--;
            }
          }
        }
      }
    }
    for (int i = 0; (i < features.size()); i++) {
      {
        Feature oneFeature = features.get(i);
        String paraName = features.get(i).getName();
        FeatureClassifier type = features.get(i).getFeatureClassifier();
        String typeName = "";
        if ((type instanceof NamedElementImpl)) {
          typeName = ((NamedElementImpl) type).getName();
          typeName = ParamUtils.transDataAADL2C(typeName);
        } else {
          typeName = type.toString();
        }
        if (((!typeName.equals("")) && (!paraName.equals("")))) {
          int _size = features.size();
          int _minus = (_size - 1);
          boolean _equals = (i == _minus);
          if (_equals) {
            boolean _equals_1 = typeName.equals("ucMisNum.impl");
            if (_equals_1) {
              int a = 0;
            }
            String _result = result;
            String _convertNo2Low = StringUtils.convertNo2Low(typeName);
            String _plus = (_convertNo2Low + " ");
            String _plus_1 = (_plus + paraName);
            result = (_result + _plus_1);
          } else {
            boolean _equals_2 = typeName.equals("ucMisNum.impl");
            if (_equals_2) {
              int a_1 = 0;
            }
            String _result_1 = result;
            String _convertNo2Low_1 = StringUtils.convertNo2Low(typeName);
            String _plus_2 = (_convertNo2Low_1 + " ");
            String _plus_3 = (_plus_2 + paraName);
            String _plus_4 = (_plus_3 + ",");
            result = (_result_1 + _plus_4);
          }
        }
      }
    }
    return StringUtils.convertNo2Low(result);
  }
  
  @Deprecated
  public static String getParasList(final Feature feature) {
    String paras = "";
    EList<Feature> features = feature.getClassifier().getAllFeatures();
    String result = "";
    for (int i = 0; (i < features.size()); i++) {
      {
        Feature oneFeature = features.get(i);
        if ((oneFeature instanceof ParameterImpl)) {
          boolean _isOut = ((ParameterImpl) oneFeature).isOut();
          if (_isOut) {
            features.remove(i);
          }
        }
      }
    }
    for (int i = 0; (i < features.size()); i++) {
      {
        Feature oneFeature = features.get(i);
        String paraName = features.get(i).getName();
        FeatureClassifier type = features.get(i).getFeatureClassifier();
        String typeName = "";
        if ((type instanceof NamedElementImpl)) {
          typeName = ((NamedElementImpl) type).getName();
        } else {
          typeName = type.toString();
        }
        if (((!typeName.equals("")) && (!paraName.equals("")))) {
          int _size = features.size();
          int _minus = (_size - 1);
          boolean _equals = (i == _minus);
          if (_equals) {
            String _result = result;
            result = (_result + ((typeName + " ") + paraName));
          } else {
            String _result_1 = result;
            result = (_result_1 + (((typeName + " ") + paraName) + ","));
          }
        }
      }
    }
    return result;
  }
  
  public static Object getParam(final Classifier classifier) {
    return null;
  }
  
  public static String getReturnValue(final EList<Feature> features) {
    String result = "";
    for (int i = 0; (i < features.size()); i++) {
      {
        Feature oneFeature = features.get(i);
        FeatureClassifier type = features.get(i).getFeatureClassifier();
        if (((oneFeature instanceof ParameterImpl) && (type instanceof NamedElementImpl))) {
          boolean _isOut = ((ParameterImpl) oneFeature).isOut();
          if (_isOut) {
            return oneFeature.getName();
          }
        } else {
          return result;
        }
      }
    }
    return result;
  }
  
  public static String getReturnValue(final Classifier classifier) {
    EList<Feature> features = classifier.getAllFeatures();
    String result = "";
    for (int i = 0; (i < features.size()); i++) {
      {
        Feature oneFeature = features.get(i);
        FeatureClassifier type = features.get(i).getFeatureClassifier();
        if (((oneFeature instanceof ParameterImpl) && (type instanceof NamedElementImpl))) {
          boolean _isOut = ((ParameterImpl) oneFeature).isOut();
          if (_isOut) {
            return oneFeature.getName();
          }
        } else {
          return result;
        }
      }
    }
    return result;
  }
}
